package XynLeen.GraduationProject.Activities;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonAdapter;
import org.litepal.crud.DataSupport;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import XynLeen.GraduationProject.Bean.BudgetBean;
import XynLeen.GraduationProject.Bean.UserBean;
import XynLeen.GraduationProject.R;
import XynLeen.GraduationProject.view.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_Bills extends BaseActivity implements AdapterView.OnItemClickListener{

    @Bind(R.id.ib_navigation_back)
    ImageButton mIbNavigation_back;
    @Bind(R.id.ib_navigation_add)
    ImageButton mIbNavigation_add;
    @Bind(R.id.tv_navigation_label)
    TextView mTvNavigation;
    @Bind(R.id.number_in)
    TextView mTvIn;
    @Bind(R.id.number_out)
    TextView mTvOut;
    @Bind(R.id.number_all)
    TextView mTvAll;
    @Bind(R.id.number_left)
    TextView mTvLeft;
    @Bind(R.id.list_view)
    ListView mListView;

    public CommonAdapter<BudgetBean> quickAdapter;

    BudgetBean mBudget;

    public SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        initQuickAdapter();

        mListView.setOnItemClickListener(this);
        mTvNavigation.setText("婚礼预算");


    }

    @Override
    public void onResume() {
        super.onResume();
        initData();

    }

    public void initQuickAdapter() {

        quickAdapter = new CommonAdapter<BudgetBean>(this, R.layout.adapter_listview_bills, null) {

            @Override
            public void onUpdate(BaseAdapterHelper helper, BudgetBean bean, int position) {

                ImageView mImg=helper.getView(R.id.iv_icon);
                mImg.setImageResource(bean.getImage());
                TextView mType = helper.getView(R.id.typename);
                mType.setText(bean.getType());
                TextView mCost = helper.getView(R.id.money);
                mCost.setText(bean.getCost());
                TextView mDate=helper.getView(R.id.time);
                mDate.setText(bean.getDate());
                TextView mNote=helper.getView(R.id.note);
                mNote.setText(bean.getRemark());
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

                Button mEdit=helper.getView(R.id.btnEdit);
                //Button mDelete=helper.getView(R.id.btnDelete);

                //修改预算
                final BudgetBean bean1 = quickAdapter.getItem(position);
                mEdit.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Activity_Bills.this, Activity_Bills_Add.class);
                        intent.putExtra("bean", bean1);
                        intent.putExtra("title", "修改");
                        //Log.e("qqqq","qqqq");
                        startActivity(intent);
                    }
                });
                //删除预算 getID报空指针
//                mDelete.setOnClickListener(new View.OnClickListener(){
//                    @Override
//                    public void onClick(View view){
//                        final AlertDialog dialog = new AlertDialog.Builder(Activity_Bills.this)
//                                .setTitle("提示")//设置对话框的标题
//                                .setMessage("确定删除吗？")//设置对话框的内容
//                                //设置对话框的按钮
//                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//                                        dialog.dismiss();
//                                    }
//                                })
//                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//                                        DataSupport.delete(BudgetBean.class, mBudget.getId());
//                                        finish();
//                                        dialog.dismiss();
//                                    }
//                                }).create();
//
//                        dialog.show();
//                    }
//                });

            }
        };
    }

    public void initData() {

        List<BudgetBean> list = DataSupport.order("date").find(BudgetBean.class);
        quickAdapter.clear();
        mListView.setAdapter(quickAdapter);
        quickAdapter.addAll(list);
        quickAdapter.notifyDataSetChanged();

        //查询预算
        UserBean bean=DataSupport.find(UserBean.class,1);
        double allnum=bean.getBudget();
        mTvAll.setText(String.valueOf(allnum));

        int number=0;
        Cursor b=DataSupport.findBySQL("select * from budgetbean");
        number=b.getCount();
        if (number!=0){
            double outnum=Double.parseDouble(DataSupport.where("cost<0").sum(BudgetBean.class,"cost",String.class));
            mTvOut.setText(String.valueOf(outnum));
            double innum=Double.parseDouble(DataSupport.where("cost>0").sum(BudgetBean.class,"cost",String.class));
            mTvIn.setText(String.valueOf(innum));
            double leftnum=allnum+outnum+innum;
            mTvLeft.setText(String.valueOf(leftnum));
        }else {
            mTvOut.setText(String.valueOf(0));
            mTvIn.setText(String.valueOf(0));
            mTvLeft.setText(String.valueOf(allnum));
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final BudgetBean bean = quickAdapter.getItem(position);
        Intent intent = new Intent(this, Activity_Bills_Add.class);
        intent.putExtra("bean", bean);
        intent.putExtra("title", "修改");
        startActivity(intent);
    }

    @OnClick({R.id.ib_navigation_back,R.id.ib_navigation_add})
    public void OnClick(View v){
        switch (v.getId()){
            case R.id.ib_navigation_back:
                finish();
                break;
            case R.id.ib_navigation_add:
                Intent intent = new Intent(this, Activity_Bills_Add.class);
                startActivity(intent);
                break;

        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_bills;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void initView() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

}
